#ifndef RESPONSIVEFILEITEM_H
#define RESPONSIVEFILEITEM_H

#include <QWidget>
#include <QPaintEvent>
#include <QMouseEvent>
#include <QEnterEvent>
#include <QPropertyAnimation>
#include <QGraphicsDropShadowEffect>
#include <QTimer>
#include <QPainter>
#include <QPainterPath>
#include <QLinearGradient>
#include <QRadialGradient>
#include "unifiedfileinfo.h"
#include "responsivefilelistengine.h"

/**
 * 🎨 ResponsiveFileItem - Sony Ive 响应式文件项
 *
 * 核心设计哲学：
 * - 渐进式信息显示：根据DisplayMode智能调整信息密度
 * - 优雅的视觉过渡：Sony风格的微妙渐变和阴影
 * - 触觉反馈式交互：流畅的动画响应
 *
 * 信息显示策略：
 * - Micro模式：图标 + 超简文件名
 * - Compact模式：+ 状态指示
 * - Standard模式：+ 进度 + 页码
 * - Expanded模式：+ 布局模式
 * - Full模式：完整信息展示
 */

// Sony Ive设计常量
#define ICON_SIZE_SMALL   16
#define ICON_SIZE_MEDIUM  24
#define ICON_SIZE_LARGE   32
#define MARGIN_SMALL      6
#define MARGIN_MEDIUM     12
#define MARGIN_LARGE      16
#define BORDER_RADIUS     8

class ResponsiveFileItem : public QWidget
{
    Q_OBJECT
    Q_PROPERTY(double animationOpacity READ animationOpacity WRITE setAnimationOpacity)
    Q_PROPERTY(double hoverScale READ hoverScale WRITE setHoverScale)
    Q_PROPERTY(double selectionScale READ selectionScale WRITE setSelectionScale)

public:
    explicit ResponsiveFileItem(const UnifiedFileInfo& fileInfo,
                                ResponsiveFileListEngine* engine = nullptr,
                                QWidget* parent = nullptr);
    ~ResponsiveFileItem() override = default;

    // === 数据管理 ===
    void setFileInfo(const UnifiedFileInfo& fileInfo);
    const UnifiedFileInfo& getFileInfo() const { return m_fileInfo; }

    // === 显示模式管理 ===
    void setDisplayMode(SonyIveDesign::DisplayMode mode);
    SonyIveDesign::DisplayMode getDisplayMode() const { return m_currentMode; }

    // === 状态管理 ===
    void setSelected(bool selected);
    bool isSelected() const { return m_isSelected; }

    void setHovered(bool hovered);
    bool isHovered() const { return m_isHovered; }

    void setPressed(bool pressed);
    bool isPressed() const { return m_isPressed; }

    // === 响应式引擎 ===
    void setResponsiveEngine(ResponsiveFileListEngine* engine);
    ResponsiveFileListEngine* getResponsiveEngine() const { return m_engine; }

    // === 动画属性 ===
    double animationOpacity() const { return m_animationOpacity; }
    void setAnimationOpacity(double opacity);

    double hoverScale() const { return m_hoverScale; }
    void setHoverScale(double scale);

    double selectionScale() const { return m_selectionScale; }
    void setSelectionScale(double scale);

    // === QWidget接口 ===
    QSize sizeHint() const override;
    QSize minimumSizeHint() const override;

signals:
    void clicked(ResponsiveFileItem* item, Qt::KeyboardModifiers modifiers);
    void doubleClicked(ResponsiveFileItem* item);
    void rightClicked(ResponsiveFileItem* item, const QPoint& globalPos);
    void dragStartRequested(ResponsiveFileItem* item, const QPoint& startPos);
    void displayModeChanged(SonyIveDesign::DisplayMode newMode);

protected:
    void paintEvent(QPaintEvent* event) override;
    void mousePressEvent(QMouseEvent* event) override;
    void mouseReleaseEvent(QMouseEvent* event) override;
    void mouseDoubleClickEvent(QMouseEvent* event) override;
    void mouseMoveEvent(QMouseEvent* event) override;
    void enterEvent(QEnterEvent* event) override;
    void leaveEvent(QEvent* event) override;
    void resizeEvent(QResizeEvent* event) override;

private slots:
    void onHoverAnimationFinished();
    void onSelectionAnimationFinished();
    void onBackgroundCacheUpdate();

private:
    // === Sony设计风格绘制方法 ===
    void drawSonyBackground(QPainter& painter);
    void drawAdaptiveIcon(QPainter& painter);
    void drawAdaptiveText(QPainter& painter);
    void drawProgressIndicator(QPainter& painter);
    void drawStatusBadge(QPainter& painter);
    void drawLayoutModeIndicator(QPainter& painter);
    void drawSelectionOverlay(QPainter& painter);
    void drawMicroOverlay(QPainter& painter);

    // === 智能文本处理 ===
    QString getAdaptiveFileName() const;
    QString getAdaptiveFileName(int availableWidth, const QFont& font) const;
    QString getStatusText() const;
    QString getLayoutModeText() const;
    QString getPageCountText() const;
    QString getSmartEllipsis(const QString& text, int maxWidth, const QFont& font) const;

    // === 布局计算 ===
    QRect getIconRect() const;
    QRect getTextRect() const;
    QRect getProgressRect() const;
    QRect getStatusRect() const;
    QRect getStatusBadgeRect() const;
    QRect getLayoutModeRect() const;
    QFont getAdaptiveFont() const;
    QIcon getStatusIcon() const;

    // === 动画系统 ===
    void initializeAnimations();
    void animateHover(bool isEntering);
    void animateSelection(bool isSelecting);
    void animateModeTransition();

    // === 视觉效果 ===
    void updateDropShadow();
    void updateBackgroundCache();
    QPixmap createCachedBackground();

    // === 性能优化 ===
    void enablePerformanceMode(bool enabled);
    void updateDisplayMode();
    SonyIveDesign::DisplayMode calculateDisplayMode(int width) const;
    bool shouldUseComplexEffects() const;

    // === 核心成员变量 ===
    UnifiedFileInfo m_fileInfo;
    ResponsiveFileListEngine* m_engine;
    SonyIveDesign::DisplayMode m_currentMode;

    // === 状态管理 ===
    bool m_isSelected;
    bool m_isHovered;
    bool m_isPressed;
    bool m_isDragStarted;

    // === 动画属性 ===
    double m_animationOpacity;
    double m_hoverScale;
    double m_selectionScale;

    // === 动画系统 ===
    QPropertyAnimation* m_hoverAnimation;
    QPropertyAnimation* m_selectionAnimation;
    QPropertyAnimation* m_opacityAnimation;

    // === 视觉效果 ===
    QGraphicsDropShadowEffect* m_dropShadowEffect;
    QPixmap m_cachedBackground;
    bool m_backgroundCacheDirty;

    // === 性能优化 ===
    QTimer* m_backgroundCacheTimer;
    bool m_performanceMode;

    // === 交互状态 ===
    QPoint m_dragStartPos;
    Qt::KeyboardModifiers m_pressModifiers;

    // === 缓存和优化 ===
    mutable QSize m_cachedSizeHint;
    mutable bool m_sizeHintCached;

    // === 布局常量（根据DisplayMode动态调整） ===
    struct LayoutMetrics {
        int iconSize;
        int padding;
        int spacing;
        int borderRadius;
        QFont primaryFont;
        QFont secondaryFont;

        void updateForMode(SonyIveDesign::DisplayMode mode);
    };

    mutable LayoutMetrics m_layoutMetrics;
    mutable bool m_layoutMetricsCached;
};

/**
 * ResponsiveFileItemFactory - 响应式文件项工厂
 *
 * 提供统一的文件项创建接口，确保一致的配置和性能优化
 */
class ResponsiveFileItemFactory
{
public:
    static ResponsiveFileItem* createItem(const UnifiedFileInfo& fileInfo,
                                         ResponsiveFileListEngine* engine,
                                         SonyIveDesign::DisplayMode mode = SonyIveDesign::DisplayMode::Standard,
                                         QWidget* parent = nullptr);

    static void configureForPerformance(ResponsiveFileItem* item, bool enablePerformanceMode);

    static void applyTheme(ResponsiveFileItem* item, const QString& themeName = "default");

    // 批量创建优化
    static QList<ResponsiveFileItem*> createBatch(const QList<UnifiedFileInfo>& fileInfos,
                                                 ResponsiveFileListEngine* engine,
                                                 QWidget* parent = nullptr);

private:
    static void applyDefaultSettings(ResponsiveFileItem* item);
};

#endif // RESPONSIVEFILEITEM_H